package com.epro.dx.util;

import android.content.Context;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.epro.dx.R;
import com.epro.dx.widget.CustomDialog;

/**
 * 自定义对话框
 * @author Zenglw
 * @version 1.0
 * @date 2017-6-15
 */

public class DialogUtils {

    private static DialogUtils instance;
    private final Context mContext;
    private int inputType;
    private String etContent;
    private int width;
    private View contentView;
    private String title;
    private CustomDialog dialog;
    private String hint;


    public String getEtContent() {
        return etContent;
    }

    public void setEtContent(String etContent) {
        this.etContent = etContent;
    }

    public CustomDialog getDialog() {
        return dialog;
    }

    public View getContentView() {
        return contentView;
    }

    private DialogUtils(Builder builder) {
        this.contentView = builder.contentView;
        this.title = builder.title;
        this.mContext = builder.context;
        this.width = builder.width;
        this.etContent = builder.etContent;
        this.inputType = builder.inputType;
        this.hint = builder.hint;
    }

    public void showDialog() {
        final CustomDialog dialog = new CustomDialog(mContext, 0, 0, contentView, R.style.DialogTheme);
        dialog.show();
        //获得dialog所在的窗体对象
        Window window = dialog.getWindow();
        //得到参数
        WindowManager.LayoutParams params = window.getAttributes();
        //动态设置dialog的宽度为屏幕宽度的3/4
        //默认的width为屏幕宽度的四分之三
        if (width == 0) {
            width = mContext.getResources().getDisplayMetrics().widthPixels * 4 / 5;
        }
        params.width = width;
        //把设置好的参数重新设置给dialog
        window.setAttributes(params);
        //最后才添加布局
        final TextView tv = (TextView) contentView.findViewById(R.id.dialog_update_new_fun_tv);
        if (tv != null) tv.setText(etContent);

        final TextView tv2 = (TextView) contentView.findViewById(R.id.edit_remark_et);
        if (tv2 != null) tv2.setText(etContent);

        window.setContentView(contentView);


        this.dialog = dialog;
    }

    public void dismiss() {
        this.dialog.dismiss();
    }

    public static final class Builder {
        Context context;
        View contentView;
        String title;
        private int width;
        private String etContent;
        private int inputType;
        public String hint;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setContentView(View view) {
            this.contentView = view;
            return this;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setWidth(int width) {
            this.width = width;
            return this;
        }

        public Builder setInputType(int inputType) {
            this.inputType = inputType;
            return this;
        }

        public Builder setHint(String hint) {
            this.hint = hint;
            return this;
        }

        public Builder setEtContent(String etContent) {
            this.etContent = etContent;
            return this;
        }

        public Builder setViewClick(final View.OnClickListener listener, int... ids) {
            if (contentView == null) {
                throw new NullPointerException("还没有设置view，必须先调用setContenView方法");
            }

            for (int id : ids) {
                if (id != -1) contentView.findViewById(id).setOnClickListener(listener);
            }

//            final EditText et = (EditText) contentView.findViewById(R.id.edit_remark_et);
//            if (et != null) {
//
//                et.addTextChangedListener(new TextWatcher() {
//                    @Override
//                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//                    }
//
//                    @Override
//                    public void onTextChanged(CharSequence s, int start, int before, int count) {
//                        contentView.setTag(s.toString());
//                    }
//
//                    @Override
//                    public void afterTextChanged(Editable s) {
//
//                    }
//                });
//            }

            return this;
        }

        public DialogUtils build() {
            return new DialogUtils(this);
        }
    }


}
